<template>
  <!--  <h2>归档章管理</h2>-->
  <div class="app-container">
    <div>
      <!-- <tabV></tabV> -->
    </div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="门类" prop="categoryId">
        <treeselect v-model="categoryId" :options="deptOptions" clearable style="width: 240px" :normalizer="normalizerCategoryID"
          noResultsText="暂无数据" placeholder="请选择档案门类" :disable-branch-nodes="true" @select="onChangeSelect" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8" ref="refbut">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="tableDataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" :index="table_index" width="55" align="center" prop="id" />
      <el-table-column label="门类" prop="category" align="center" />
      <el-table-column label="启用状态" align="center">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.state" active-value="1" inactive-value="0"
            @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="颜色" prop="color" align="center" />
      <el-table-column label="字体" prop="font" align="center" />
      <el-table-column label="边框粗细" prop="fontBold" align="center" />
      <el-table-column label="总体高度(cm)" prop="totalHeight" align="center" />
      <el-table-column label="总体宽度(cm)" prop="totalWidth" align="center" />
      <el-table-column label="操作" align="center" width="250">
        <template slot-scope="scope">
          <el-button type="info" size="mini" plain @click="onPreviewTap(scope.row)">预览</el-button>
          <el-button type="success" size="mini" plain @click="handleUpdate(scope.row)">修改</el-button>
          <el-button type="danger" size="mini" plain @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <FilingSeal v-if="seal.open" :seal="seal" @closeDelDialog="sealCancel" @submitForm="submitForm" :dataInfo="dataInfo">
    </FilingSeal>

    <el-dialog id="dialogPdf" title="预览" :visible.sync="open" width="55%" append-to-body :before-close="closeTap">
      <div style="width:100%;margin:0 auto">
        <pdfDemo  :url="pdfUrl"   operate="online" style="height:740px"></pdfDemo>

      </div>
    </el-dialog>
  </div>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { deleteSeal, getSealList, listGROUPTree, updateSeal,deleteTempFile } from "@/api/stage/stageManagement/archiveSeal";
import FilingSeal from "@/views/stage/stageManagement/archiveSeal/filingSeal";
import pdfDemo from '@/components/componentsPdf/index1.vue'
import axios from "axios";
import { getToken } from "@/utils/auth";
export default {
  name: "index",
  components: { FilingSeal, Treeselect,pdfDemo },
  data() {
    return {
      open:false,
      pdfUrl:"",

      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      categoryId: undefined,
      // 显示搜索条件
      showSearch: true,
      // 遮挡层
      loading: true,
      // 表格数据
      tableDataList: [],
      // 总条数
      total: 0,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 树选项
      deptOptions: [],

      seal: {
        title: '',
        open: false
      },
      dataInfo: {}
    }
  },
  created() {
    this.getDeptTree()
  },
  methods: {

    // 获取档案门类树
    getDeptTree() {
      listGROUPTree().then(response => {
        this.deptOptions = response.data;
        this.getList()
      })
    },
    // normalizer(node) {
    //   if (node.type == null) {
    //     if (node.children == undefined) {
    //       node["children"] = undefined
    //       node['isDisabled'] = true;
    //     }
    //   }
    // },





          // 只能选择数据下面有children 的数据，没有children的时候禁用
          normalizerCategoryID(node) {
            if (node.nodeType == 'fonds') {
                if (node.children == undefined || node.children == null || !node.children.length) {

                    node['isDisabled'] = true;
                }

            }
            if (node.children == null || node.children == undefined || !node.children.length) {
                delete node.children;
            }
            return node
        },



    onChangeSelect(node, instanceId) {
      this.queryParams.category = node.label
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if (this.categoryId == undefined) {
        this.queryParams.category = undefined
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.categoryId = undefined
      this.queryParams.category = undefined
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getList() {
      this.loading = true
      getSealList(this.queryParams).then(response => {
        this.tableDataList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    table_index(index) {
      return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1;
    },
    // 增加项目
    handleAdd() {
      this.dataInfo = undefined
      this.seal.title = '新增归档章'
      this.seal.open = true
    },
    // 修改
    handleUpdate(row) {
      this.dataInfo = row
      this.seal.title = '修改归档章'
      this.seal.open = true
    },
    // 是否启用
    handleStatusChange(row) {
      this.form = {
        state: row.state,
        id: row.id
      }
      updateSeal(this.form).then(response => {
        this.$modal.msgSuccess('修改状态成功')
        this.getList()
      })
    },
     //关闭预览
     closeTap() {
      var that = this;
      that.open = false;
      deleteTempFile().then(res=>{

      })
    },
    onPreviewTap(row) {
      console.log(row,"sldkfjklsdjfkl")
      var that = this;
      that.open = true
      axios({
        method: 'POST',
        url: process.env.VUE_APP_BASE_API + `/archive/sealmanage/viewSeal2?id=`+row.id,
        headers: {
          Authorization: "Bearer " + getToken()
        },
        responseType: 'blob' // 更改responseType类型为 blob
      }).then(res => {
        const blob = new Blob([res.data], { type: 'application/pdf;charset-UTF-8' });
          this.pdfUrl = blob
      }).catch(err => {
        console.log(err)
      })
    },
    // 保存
    submitForm() {
      this.seal.open = false
      this.getList()
    },
    // 取消
    sealCancel() {
      this.seal.open = false
    },
    // 删除
    handleDelete(row) {
      const id = row.id
      this.$modal.confirm('是否确认删除所选数据项？').then(function () {
        return deleteSeal(id)
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
  }
}
</script>

<style scoped></style>
